package org.yanglk.demo.redis.service;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

/**
 * redis缓存设置公共类
 * 
 * @author yanglk <br>
 *         Create Time：2018年7月9日下午5:35:03<br>
 *         <a href="mailto:yanglk2010@sina.com">yanglk2010@sina.com</a>
 * @since 1.0
 * @version 1.0
 */
@Service
public class RedisSourceManager<VALUE>{

	public static final String LIGHTNING_REDIS_PREFIX_ = "redis_prefix_";

	@Autowired
	@Qualifier("redisTemplate")
	private RedisTemplate<String, VALUE> redisTemplate;

	public RedisSourceManager() {
	}

	public void removeByKey(String key) {
		redisTemplate.delete(key);
	}

	public void saveCacheMap(String key, Map<String, VALUE> map) {
		HashOperations<String, String, VALUE> hashOperations = redisTemplate.<String, VALUE>opsForHash();
		if (null != map) {
			for (Map.Entry<String, VALUE> entry : map.entrySet()) {
				hashOperations.put(key, entry.getKey(), entry.getValue());
			}
		}
	}
	
	public void saveCacheMap(String key, String hashKey, VALUE hashValue) {
		HashOperations<String, String, VALUE> hashOperations = redisTemplate.<String, VALUE>opsForHash();
		hashOperations.put(key, hashKey, hashValue);
	}

	/**
	 * 获得缓存的Map
	 * 
	 * @param key
	 * @param hashOperation
	 * @return
	 */
	public Map<String, VALUE> getCacheMap(String key) {
		Map<String, VALUE> map = redisTemplate.<String, VALUE>opsForHash().entries(key);
		return map;
	}

	public void saveCacheList(String key, List<VALUE> list) {
		ListOperations<String, VALUE> listOperations = redisTemplate.opsForList();
		if (null != list) {
			list.forEach(v -> {
				listOperations.rightPush(key, v);
			});
		}
	}

	public List<VALUE> getCacheList(String key) {
		List<VALUE> list = redisTemplate.opsForList().range(key, 0, Integer.MAX_VALUE);
		return list;
	}

	@SuppressWarnings("unchecked")
	public void saveCacheSet(String key, List<VALUE> list) {
		SetOperations<String, VALUE> setOperations = redisTemplate.opsForSet();
		if (null != list) {
			list.forEach(v -> {
				setOperations.add(key, v);
			});
		}
	}

	@SuppressWarnings("unchecked")
	public void saveCacheSet(String key, VALUE v) {
		SetOperations<String, VALUE> setOperations = redisTemplate.opsForSet();
		setOperations.add(key, v);
	}

	public Set<VALUE> getCacheSet(String key) {
		Set<VALUE> set = redisTemplate.opsForSet().members(key);
		return set;
	}

	public void saveCacheValue(String key, VALUE value) {
		ValueOperations<String, VALUE> valueOperations = redisTemplate.opsForValue();
		valueOperations.set(key, value);
	}

	public VALUE getCacheValue(String key) {
		VALUE value = redisTemplate.opsForValue().get(key);
		return value;
	}
}
